package com.liurz.mongdb.service;

import com.google.common.collect.Lists;
import com.liurz.mongdb.dao.SensorMongoRepository;
import com.liurz.mongdb.model.Sensor;
import com.liurz.mongdb.vo.CreateSensorDto;
import com.liurz.mongdb.vo.ResultModel;
import com.liurz.mongdb.vo.ResultModelUtils;
import com.liurz.mongdb.vo.UpdateSensorDto;
import com.mongodb.client.MongoClient;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service("mongoSensorService")
public class SensorServiceImpl implements SensorService {

    @Resource(name = "mongoSensorRepository")
    private SensorMongoRepository sensorMongoRepository;

    @Autowired
    MongoTemplate mongoTemplate;

    MongoClient mongoClient;



    @Override
    public ResultModel save(Sensor sensor) {
        sensorMongoRepository.saveSingle(sensor);
        return  ResultModelUtils.getSuccessInstance("保存成功");
    }

    @Override
    public ResultModel getSensorById(String id) {
        log.info("getSensorById --> {}");
        Sensor sensor;
        try {
            sensor = sensorMongoRepository.queryById(id);
        } catch (Exception e) {
            log.error("查询传感器详情失败", e.getMessage());
            throw new RuntimeException("查询传感器详情失败");
        }
        return ResultModelUtils.getSuccessInstance(sensor);
    }


    @Override
    public ResultModel deleteSensor(String id) {
        log.info("deleteSensor --> {}");
        sensorMongoRepository.deleteById(id);
        return ResultModelUtils.getSuccessInstance("删除传感器成功");
    }


    @Override
    public ResultModel getSensorList() throws Exception {
        log.info("getSensorList --> {}");
        List<Sensor> sensorList = Lists.newArrayList();
        try {
            Sensor param = new Sensor();
            sensorList = sensorMongoRepository.queryList(param);
        } catch (Exception e) {
            log.error("查询传感器列表失败", e.getMessage());
            throw new Exception("查询传感器列表失败");
        }
        return ResultModelUtils.getSuccessInstance("SUCCESS");
    }



}